package com.dragon.client.controller.supplies;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.supplies.IPurchaseRequisitionService;
import com.dragon.repository.domain.dto.ProductDTO;
import com.dragon.repository.domain.dto.PurchaseRequisitionDTO;
import com.dragon.repository.domain.vo.PurchaseRequisitionVO;
import com.dragon.repository.entity.Product;
import com.dragon.repository.entity.PurchaseRequisition;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 请购单 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-11-15
 */
@RestController
@RequestMapping("purchaseRequisition")
@Api(tags = "请购")
public class PurchaseRequisitionController {

    @Autowired
    IPurchaseRequisitionService service;

    @GetMapping("list")
    @ApiOperation(value = "请购列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "id", value = "详情id", required = false, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数（标题）", required = false, type = "string")
    })
    public ResponseData<List<PurchaseRequisitionDTO>> listPurchase(@RequestParam Integer page,
                                                                   @RequestParam Integer limit,
                                                                   @RequestParam(required = false) Integer id,
                                                                   @RequestParam(required = false) String search){
        Page<PurchaseRequisition> requisitionPage = service.getPurchaseList(page,limit,id,search);
        return ResponseData.success(PurchaseRequisitionDTO.convertList(requisitionPage.getRecords()), requisitionPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    @AddPage
    public ResponseData addPurchase(@RequestBody @Validated(Insert.class) PurchaseRequisitionVO vo){
        return ResponseData.success(service.addPurchase(PurchaseRequisitionVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updatePurchase(@RequestBody @Validated(Update.class)PurchaseRequisitionVO vo){
        service.updatePurchase(PurchaseRequisitionVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public ResponseData deleteById(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                   @RequestParam Integer id){
        service.deleteById(id);
        return ResponseData.success();
    }

    @GetMapping("viewItem")
    @ApiOperation(value = "可根据生单状态查看请购详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "字符串ids", required = true, type = "string"),
            @ApiImplicitParam(name = "generateState", value = "生单状态(1未生单 2已生单)", required = false, type = "int")
    })
    public ResponseData<List<PurchaseRequisitionDTO>> getListDetail(@RequestParam String ids,
                                                                    @RequestParam(required = false) Integer generateState){
        List<PurchaseRequisition> listDetail = service.getListDetail(ids, generateState);
        return ResponseData.success(PurchaseRequisitionDTO.convertList(listDetail));
    }

    @PostMapping("cancel")
    @ApiOperation(value = "取消订单")
    public ResponseData cancel(@ApiParam(name = "ids", value = "取消订单的请购产品主id", required = true, type = "string")
                               @RequestParam String ids){
        service.cancel(ids);
        return ResponseData.success();
    }

    @GetMapping("productList")
    @ApiOperation(value = "商品spu列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "categoryId", value = "类别id", required = false, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数(名称)", required = false, type = "string")
    })
    public ResponseData<List<ProductDTO>> productList(@RequestParam Integer page,
                                                      @RequestParam Integer limit,
                                                      @RequestParam(required = false) Integer categoryId,
                                                      @RequestParam(required = false) String search){
        Page<Product> productPage = service.productSpuList(page, limit, categoryId, search);
        return ResponseData.success(ProductDTO.convertList(productPage.getRecords()), productPage.getTotal());
    }

}
